package com.acertificate.domain;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.TableRow;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.acertificate.entities.AbsWrap;

public class Comwanmv extends Domain{

	public static final String URL_PREFIX = "http://x.wanmv.com/article/list_5_";
	public static final String SUFFIX = ".html";

	public static final String SQL_TEMPLATE = "INSERT INTO db_content_article VALUES (NULL, '%s', '0', '%s', '', '', '', '', '', '1', '2', '0', '1331793273', '0', '1', '1', '6', '0');";

	public static int maxProcess = 20;
	public static int total = 379;
	public static int threadCount;

	{
		threadCount = total / maxProcess + (total % maxProcess == 0 ? 0 : 1);
	}

	public static void main(String[] args) throws ParserException, MalformedURLException, IOException {
		Comwanmv wanmv = new Comwanmv();
		for (int i = 1; i <= threadCount; i++) {
			ComwanmvThread thread = wanmv.new ComwanmvThread((i - 1) * maxProcess + 1, (i * maxProcess > total ? total : i * maxProcess));
			thread.start();
		}

	}

	class ComwanmvThread extends Thread {

		public int start;
		public int end;

		public ComwanmvThread() {
		}

		public ComwanmvThread(int start, int end) {
			this.start = start;
			this.end = end;
		}

		public void run() {
			for (int i = start; i < end; i++) {
				getManifest(i, URL_PREFIX + i + SUFFIX);
			}
		}

		public List<AbsWrap> getManifest(int index, String url) {
			List<AbsWrap> results = new ArrayList<AbsWrap>();
			try {
				URLConnection urlConnection = new URL(url).openConnection();
				Parser parser = new Parser(urlConnection);
				AndFilter andFilter = new AndFilter(new NodeClassFilter(LinkTag.class), new HasAttributeFilter("class", "title"));
				NodeList list = parser.extractAllNodesThatMatch(andFilter);
				for (int i = 0; i < list.size(); i++) {
					Node tag = list.elementAt(i);
					if (tag instanceof LinkTag) {
						LinkTag link = (LinkTag) tag;
						AbsWrap wrap = new AbsWrap();
						wrap.setTitle(link.getLinkText());
						wrap.setUrl(link.getLink());
						wrap.setContent(getContent(link.getLink()));
						results.add(wrap);
					}
				}
				StringBuffer sb = new StringBuffer();
				for (AbsWrap wrap : results) {
					sb.append(String.format(SQL_TEMPLATE, new Object[] { wrap.getTitle(), wrap.getContent() }));
					sb.append("\r\n\r\n");
				}
				try {
					String fileName = "p-" + index + ".txt";
					System.out.println("generating SQL files " + fileName + " ...");
					IOUtils.write(sb.toString(), new FileOutputStream(new File("c:\\domains\\" + fileName)));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			return results;
		}

		public String getContent(String url) throws ParserException {
			String content = null;
			Parser contentParser = new Parser();
			contentParser.setURL(url);
			NodeList nodes = contentParser.parse(new NodeClassFilter(TableRow.class));
			for (int i = 0; i < 1; i++) {
				Node node = nodes.elementAt(i);
				content = node.toHtml();
				content = content.substring(content.indexOf("<p>"), content.lastIndexOf("</p>") + 4);
			}
			return content;
		}
	}

}
